﻿package wangshuai.risk.dao.impl;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.stereotype.Component;

import publics.model.Balance;
import publics.model.Company;
import publics.model.EnterpriseType;
import publics.util.Page;

import wangshuai.risk.dao.CompanyDao;


import com.opensymphony.xwork2.util.ResolverUtil.IsA;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import com.sun.org.apache.regexp.internal.recompile;

import freemarker.core.ReturnInstruction.Return;

/**
 * 分公司持久层实现
 * 
 */
@Component("companyDao")
public class CompanyDaoImpl implements CompanyDao {
	
	private HibernateTemplate hibernateTemplate;// hibernate的spring代理模版

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	// 注入 hibernate模版
	@Resource(name = "hibernateTemplate")	
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
		System.out.println("hibernateTemplate injected in companyDaoImpl!");
		// 测试用，实际运行中可去掉这句
	}
	
	public Integer save(Company company) {
		return (Integer)this.getHibernateTemplate().save(company);
	}

	public boolean update(Company company) {
		try{
			this.getHibernateTemplate().update(company);
		}catch (DataAccessException e) {
			return false;
		}
		return true;
	}

	public boolean deleteById(Integer companyId) {
	//	System.out.println("DaoInpl里 companyId="+companyId);
		try{// 使用try 来确定 删除是否成功
			this.getHibernateTemplate().delete(this.getHibernateTemplate().get(Company.class, companyId));
		}catch (DataAccessException e) {
			return false;
		}
		return true;
	}
	
	 public boolean delete(Company company){
		try {
			 this.getHibernateTemplate().delete(company);
		} catch (DataAccessException e) {
			return false;
		}
	    return true;
	    	
	}
	 
	public Company findById(Integer id) {		
		return this.getHibernateTemplate().get(Company.class, id);
	}
	
	@SuppressWarnings("unchecked")
	public List<Company> findByPage(final String hql, final int offset, final int pageSize) {
		List<Company> list = this.getHibernateTemplate().executeFind(
				new HibernateCallback() {
					public Object doInHibernate(Session session) throws HibernateException ,SQLException {
						//执行hibernate分页查询
						List<Company> result = session.createQuery(hql)
							.setFirstResult(offset)
							.setMaxResults(pageSize)
							.list();
						return result;
					}
				}
				);
		
		return list;
	}
	 

	public int findByCount(String sql) {	
		return this.getHibernateTemplate().find(sql).size();
	 
	}

	@SuppressWarnings("unchecked")
	public List<Company> find(String sql){		
		return this.getHibernateTemplate().find(sql);
	}
	
	/*	public List findAll() {
}
*/

}
